//
//  AppDelegate.m
//  Utils
//
//  Created by enghou on 2018/1/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "AppDelegate.h"
#import <SVProgressHUD.h>
#import "LoginViewController.h"
#import "LocalPersistent.h"
#import <AVFoundation/AVFoundation.h>
#import "ProvincesGenerator.h"
#import "NetWorkerManager.h"
#import "LocalPersistent.h"
BMKMapManager *_mapManager;
BMKLocationManager *_locationManager;
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _mapManager = [[BMKMapManager alloc]init];
    [[BMKLocationAuth sharedInstance]checkPermisionWithKey:@"zXLMdOwsEDfIqY2PBzIzBij3fQuxw8AL" authDelegate:self];
    BOOL ret = [_mapManager start:@"zXLMdOwsEDfIqY2PBzIzBij3fQuxw8AL" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }

    _locationManager = [[BMKLocationManager alloc] init];

    _locationManager.delegate = self;

    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    _locationManager.distanceFilter = 1.0;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    _locationManager.allowsBackgroundLocationUpdates = YES;// YES的话是可以进行后台定位的，但需要项目配置，否则会报错，具体参考开发文档
    _locationManager.locationTimeout = 10;
    _locationManager.reGeocodeTimeout = 10;
    [_locationManager startUpdatingLocation];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    
    NSString *sid = [[LocalPersistent sharedInstance]contentForKey:@"sid"];
    if (!sid) {
        LoginViewController *login = [[LoginViewController alloc]init];
        self.window.rootViewController = login;
    }
    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    
    BOOL flag;
    
    NSError *setCategoryError = nil;
    
    flag = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}

- (void)onCheckPermissionState:(BMKLocationAuthErrorCode)iError
{
    NSLog(@"location auth onGetPermissionState %ld",(long)iError);
    
}

- (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager didUpdateLocation:(BMKLocation * _Nullable)location orError:(NSError * _Nullable)error{
    BOOL result = [[[LocalPersistent sharedInstance]contentForKey:@"look"]boolValue];
    if (result) {
        NSDictionary *param = @{@"location.centerlon":[NSString stringWithFormat:@"%.5f",location.location.coordinate.longitude],@"location.centerlat":[NSString stringWithFormat:@"%.5f",location.location.coordinate.latitude]};
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/gisHistoryLocationMobile/addLocation.action" parameter:param completeBlock:^(NSDictionary *source) {
            
        } failBlock:^(NSInteger code) {
            
        }];
    }
}


@end
